
const Product=require('../models/product.model');
const axios=require('axios');
const async=require('async')
module.exports={
    create:function(req,res,next){
        const product=new Product(req.body);
        product.save().then(data=>{
            res.json(data);
        })
    },
    get:function(req,res,next){
        var id=req.params.id;
        Product.findById(id).then(data=>{
            res.json(data);
        })
    },
    delete:function(req,res,next){
        var id=req.params.id;
        Product.findByIdAndRemove(id).then(data=>{
            res.json(data);
        })
    },
    removes:function(req,res,next){
        var ids=req.body.ids.split(',');
        Product.remove({_id:{ $in:ids}}).then(data=>{
            res.json('删除成功')
        })
    },
    update:function(req,res,next){
        var id=req.params.id;
        Product.findByIdAndUpdate(id,{$set: req.body},{new:false}).then(data=>{
            res.json(data);
        })
    },
    list:function(req,res,next){
        var page=req.body.page?req.body.page:1;
        var rows=req.body.rows?req.body.rows:3
        Product.paginate({}, { sort:({_id:-1}), page:+page, limit:+rows}, function(err, result) {
            result.rows=result.docs;
            delete result.docs;
            var c=JSON.parse(JSON.stringify(result));
            var arr = [];
            for (let prop of c.rows) {
                arr.push(function axiosfn(callback) {
                    axios.get('http://localhost:3000/comment/replynum/' + prop._id).then((resu) => {
                        console.log(resu.data)
                        prop.reply = resu.data;
                        callback();
                    });
                })
            }
            async.parallel(arr,function(err){
                res.json(c)
            })
          });
    },
    listid:function(req,res,next){
        var parentId=req.params.parentId;
        var page=req.body.page?req.body.page:1;
        var rows=req.body.rows?req.body.rows:3
        Product.paginate({parentId:parentId}, { sort:({_id:-1}), page:+page, limit:+rows}, function(err, result) {
            result.rows=result.docs;
            delete result.docs;

            var c=JSON.parse(JSON.stringify(result));
            var arr = [];
            for (let prop of c.rows) {
                arr.push(function axiosfn(callback) {
                    axios.get('http://localhost:3000/comment/replynum/' + prop._id).then((resu) => {
                        console.log(resu.data)
                        prop.reply = resu.data;
                        callback();
                    });
                })
            }
            async.parallel(arr,function(err){
                res.json(c)
            })
          });
    },
    listval:function(req,res,next){
        var name=req.params.name;

        var page=req.body.page?req.body.page:1;
        var rows=req.body.rows?req.body.rows:3
        Product.paginate({name:name}, { sort:({_id:-1}),page:+page, limit:+rows}, function(err, result) {
            result.rows=result.docs;
            delete result.docs;
            res.json(result);
          });
    },
    listtype:function(req,res,next){
        var goodstype=req.params.goodstype;
        var page=req.body.page?req.body.page:1;
        var rows=req.body.rows?req.body.rows:3;
        Product.paginate({goodstype:decodeURI(goodstype)}, { sort:({_id:-1}),page:+page, limit:+rows}, function(err, result) {
            result.rows=result.docs;
            delete result.docs;
            res.json(result);
          });
    },
}